<template>
  <div class="edit-popup">
    <popup
      ref="popupRef"
      :title="popupTitle"
      :async="true"
      width="700px"
      @confirm="handleSubmit"
      @close="handleClose"
    >
      <el-form
        ref="formRef"
        :model="formData"
        label-width="120px"
        :rules="formRules"
      >
        <el-form-item label="入口名称" prop="name" >
          <el-input v-model="formData.name" placeholder="请输入入口名称" />
        </el-form-item>
		<el-form-item label="首页图标" prop="logo">
<!--		    <material-picker :limit="1" v-model="formData.live_logo" />-->
			<div style="display: flex; flex-direction: column; gap: 10px">
                <el-image v-if="formData.logo" style="width: 100px; height: 100px" :src="formData.logo" />
                <!--             <material-picker :limit="9" v-model="formData.slider" />-->
                <upload
                        class="mr-3"
                        :type="'image'"
                        :show-progress="true"
                        @change="actionImg('logo')"
                        ref="coverRef"
                >
                    <el-button type="primary">本地上传</el-button>
                </upload>
            </div>
		</el-form-item>
        <el-form-item label="跳转链接" prop="url" >
          <el-input v-model="formData.url" placeholder="请输入跳转链接" />
        </el-form-item>
          <el-form-item label="排序" prop="sort" >
              <el-input v-model="formData.sort" placeholder="请输入跳转链接" />
          </el-form-item>
      </el-form>
    </popup>
  </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from "element-plus";
import { entranceList,stateEntrance,entranceAdd, detailEntrance, entranceEdit, decEntrance } from '@/api/reception'
import Popup from "@/components/popup/index.vue";
import { useDictOptions } from '@/hooks/useDictOptions'
import {ref} from "vue";
const emit = defineEmits(["success", "close"]);
const formRef = shallowRef<FormInstance>();
const popupRef = shallowRef<InstanceType<typeof Popup>>();
const mode = ref("add");
const popupTitle = computed(() => {
  return mode.value == "edit" ? "编辑" : "新增";
});
const formData = reactive({
    id:'',
    name:'',
    logo: "",
    url: "",
    sort:'',
});

const formRules = {
  name: [
    {
      required: true,
      message: "请输入口名称",
      trigger: "blur",
    },
  ],
    logo: [
    {
      required: true,
      message: "请上传首页图标",
      trigger: "blur",
    },
  ],
	url: [
    {
      required: true,
      message: "请输入跳转链接",
      trigger: "blur",
    },
  ],
    sort: [
        {
            required: true,
            message: "请输序号",
            trigger: "blur",
        },
    ],
};

const open = (type = "add") => {
  mode.value = type;
  popupRef.value?.open();
};
const setFormData = (data: Record<any, any>) => {
  for (const key in formData) {
    if (data.info[key] != null && data.info[key] != undefined) {
      //@ts-ignore
      formData[key] = data.info[key];
    }
  }
};

const getDetail = async (row: Record<string, any>) => {
  const data = await detailEntrance({
      entrance_id: row.id,
  });
  setFormData(data);
};

const handleClose = () => {
  emit("close");
};



// 照片更新
const coverRef = ref(null)  // 封面
const actionImg = async (type: string) => {
    formData.logo = coverRef.value.fileList[0].response.data.url;
};

defineExpose({
  open,
  setFormData,
  getDetail,
});
//提交
const handleSubmit = async () => {
  await formRef.value?.validate();
    mode.value == "edit"
        ? formData.entrance_id = formData.id
        : delete formData.id;
  mode.value == "edit"
    ? await entranceEdit(formData)
    : await entranceAdd(formData);
  popupRef.value?.close();
  emit("success");
};
</script>
